﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class SetRoleUserControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        MPRepeater MPRepeaterList;
        TextBox username;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack) {
                Bind();
            }
            Button saveset = (Button)FindControl("saveset");
            saveset.Click += new EventHandler(saveset_Click);
            Button search = (Button)FindControl("search");
            search.Click += new EventHandler(search_Click);
        }
        protected void InitControl() {
            MPRepeaterList = (MPRepeater)FindControl("MPRepeaterList");
            username = (TextBox)FindControl("username");
        }
        protected void Bind()
        {
            string strid = base.Page.Request["ID"];
            DataTable dt = _EluZSDataProvider.SetRoleUser(strid, username.Text.Trim());
            MPRepeaterList.DataSource = dt;
            MPRepeaterList.DataBind();
        }

        protected void saveset_Click(object sender, EventArgs e)
        {
            string strid = base.Page.Request["ID"];
            for (int temp = 0; temp < MPRepeaterList.Items.Count; temp++)
            {
                Label idtib = (Label)MPRepeaterList.Items[temp].FindControl("idtib");
                int usercount = Convert.ToInt32(_EluZSDataProvider.GetRoleUser(strid, idtib.Text.Trim()));
                CheckBox checkselect = (CheckBox)MPRepeaterList.Items[temp].FindControl("checkselect");
                if (checkselect.Checked)
                {
                    if (usercount == 0)
                    {
                        _EluZSDataProvider.UpdateRoleUser("0",strid, idtib.Text.Trim());
                    }
                }
                else
                {
                    if (usercount > 0)
                    {
                        _EluZSDataProvider.UpdateRoleUser("1", strid, idtib.Text.Trim());
                    }
                }
            }
            _EluZSDataProvider.UpdateCanSync("zs_user_in_role","roleid", strid,"");
            base.Page.Response.Redirect("SetRoleUser.aspx?id=" + base.Page.Request["id"] + "");
        }

        protected void search_Click(object sender, EventArgs e)
        {
            Bind();
        }
        
    }
}


